﻿Start-Process .\geckodriver.exe -ArgumentList '--port 9515' -WindowStyle Hidden

$url = "http://127.0.0.1:9515/session"
$d = '{"capabilities":{}}'
$r = Invoke-WebRequest -Uri $url -Method Post -ContentType 'application/json' -Body $d

Start-Sleep -Seconds 2
if($r.StatusCode -eq 200)
{
    $sid = ($r.Content | ConvertFrom-Json).value.sessionId
}

$d = '{"url":"http://cn.bing.com"}'
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/url" -Method Post -ContentType 'application/json' -Body $d
Start-Sleep -Seconds 2

$r = Invoke-WebRequest -Uri "$($url)/$($sid)/title" -Method Get
($r.Content | ConvertFrom-Json).value
Start-Sleep -Seconds 2

$d='{"x":30, "y":30, "width":300, "height":300}'
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/window/rect" -Method Post -ContentType 'application/json' -Body $d
Start-Sleep -Seconds 2

$r = Invoke-WebRequest -Uri "$($url)/$($sid)/window/maximize" -Method Post -ContentType 'application/json' -Body '{"handle":""}'
Start-Sleep -Seconds 2

$d = '{"using":"css selector", "value":".sb_form_q"}'
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/element" -Method Post -ContentType 'application/json' -Body $d 
Start-Sleep -Seconds 2

$eid = ($r.Content | ConvertFrom-Json).value.'element-6066-11e4-a52e-4f735466cecf'
$d =@"
{"text":"webdriver"}
"@
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/element/$($eid)/value" -Method Post -ContentType 'application/json' -Body $d
Start-Sleep -Seconds 2

$d = '{"using":"css selector", "value":".search.icon.tooltip"}'
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/element" -Method Post -ContentType 'application/json' -Body $d 
Start-Sleep -Seconds 2

$eid = ($r.Content | ConvertFrom-Json).value.'element-6066-11e4-a52e-4f735466cecf'
$r = Invoke-WebRequest -Uri "$($url)/$($sid)/element/$($eid)/click" -Method Post -ContentType 'application/json' -Body "{}"
Start-Sleep -Seconds 2


$r = Invoke-WebRequest -Uri "$($url)/$($sid)" -Method Delete
Start-Sleep -Seconds 2


Get-Process geckodriver | Stop-Process